package com.cms.common.model.services.impl;


import com.cms.common.model.criteria.Criteria;
import com.cms.common.model.entity.ArticleCategory;
import com.cms.common.model.repository.ArticleCategoryRepository;
import com.cms.common.model.services.ArticleCategoryService;
import com.cms.common.model.vo.RequestData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("ArticleCategoryService")
public class ArticleCategoryServiceImpl implements ArticleCategoryService {

    @Autowired
    ArticleCategoryRepository articleCategoryRepository;


    @Override
    public Page<ArticleCategory> getAllByPage(RequestData requestData) {
        Criteria<ArticleCategory> criteria = new Criteria<>();
        requestData.setObjParams(criteria);
        Sort sort = Sort.by(Sort.Order.asc("updateTime"), Sort.Order.asc("id"));
        Pageable pageable = PageRequest.of(requestData.getPage() - 1, requestData.getPageSize(), sort);
        Page<ArticleCategory> articleCategories = articleCategoryRepository.findAll(criteria, pageable);
        return articleCategories;
    }

    @Override
    public ArticleCategory getById(Long id) {
        return articleCategoryRepository.findById(id).orElse(null);
    }

    @Override
    public ArticleCategory addArticleCategory(ArticleCategory articleCategory) {
        return articleCategoryRepository.saveAndFlush(articleCategory);
    }

    @Override
    public void deleteArticleCategory(ArticleCategory articleCategory) {
        articleCategoryRepository.delete(articleCategory);
    }

    @Override
    public List<ArticleCategory> getAll() {
        return articleCategoryRepository.findAll();
    }
}
